/*
 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 *
 * This file is part of the lwIP TCP/IP stack.
 *
 * Author: Huawei Technologies
 *
 */

#ifndef __DPDK_CKSUM_H__
#define __DPDK_CKSUM_H__

#include "lwipopts.h"

#if GAZELLE_ENABLE
#include <stdbool.h>
#include <rte_ethdev.h>
#include "dpdk_version.h"

#if CHECKSUM_OFFLOAD_ALL
#include <rte_mbuf_core.h>
#include "lwip/pbuf.h"
#endif

extern uint64_t get_eth_params_rx_ol(void);
extern uint64_t get_eth_params_tx_ol(void);
#if CHECKSUM_CHECK_IP_HW
// for ip4_input
static inline u64_t is_cksum_ipbad(struct pbuf *p) {
    return p->ol_flags & (RTE_MBUF_F_RX_IP_CKSUM_BAD);
}
#endif /* CHECKSUM_CHECK_IP_HW */

#if (CHECKSUM_CHECK_TCP_HW || CHECKSUM_CHECK_UDP_HW)
// for tcp_input and udp_input
static inline u64_t is_cksum_bad(struct pbuf *p) {
    return p->ol_flags & (RTE_MBUF_F_RX_L4_CKSUM_BAD);
}
#endif /* (CHECKSUM_CHECK_TCP_HW || CHECKSUM_CHECK_UDP_HW) */

#if CHECKSUM_GEN_IP_HW
static inline void ethh_cksum_set(struct pbuf *p, u16_t len) {
    p->l2_len = len;
}

// replaces IPH_CHKSUM_SET
static inline void iph_cksum_set(struct pbuf *p, u16_t len, bool do_ipcksum) {
    p->ol_flags |= RTE_MBUF_F_TX_IPV4;
    if (do_ipcksum) {
        p->ol_flags |= RTE_MBUF_F_TX_IP_CKSUM;
    }
    p->l3_len = len;
}
#endif /* CHECKSUM_GEN_IP_HW */

// replace ip_chksum_pseudo
#if (CHECKSUM_GEN_TCP_HW || CHECKSUM_GEN_UDP_HW)
#include <rte_ip.h>

#if CHECKSUM_GEN_TCP_HW
static inline void tcph_cksum_set(struct pbuf *p, u16_t len) {
    p->l4_len = len;
    p->ol_flags |= RTE_MBUF_F_TX_TCP_CKSUM;
}
#endif /* CHECKSUM_GEN_TCP_HW */

#if CHECKSUM_GEN_UDP_HW
static inline void udph_cksum_set(struct pbuf *p, u16_t len) {
    p->l4_len = len;
    p->ol_flags |= RTE_MBUF_F_TX_UDP_CKSUM;
}
#endif /* CHECKSUM_GEN_UDP_HW */

static inline u16_t ip_chksum_pseudo_offload(u8_t proto, u16_t proto_len,
                                             const ip_addr_t *src, const ip_addr_t *dst)
{
    struct ipv4_psd_header {
        uint32_t src_addr; /* IP address of source host. */
        uint32_t dst_addr; /* IP address of destination host. */
        uint8_t  zero;     /* zero. */
        uint8_t  proto;    /* L4 protocol type. */
        uint16_t len;      /* L4 length. */
    } psd_hdr;

    psd_hdr.src_addr = ip4_addr_get_u32(src);
    psd_hdr.dst_addr = ip4_addr_get_u32(dst);
    psd_hdr.proto = proto;
    psd_hdr.len = lwip_htons(proto_len);
    psd_hdr.zero = 0;

    return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
}
#endif /* (CHECKSUM_GEN_TCP_HW || CHECKSUM_GEN_UDP_HW) */

#endif /* GAZELLE_ENABLE */
#endif /* __DPDK_CKSUM_H__ */
